<?php 

session_start();

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*	This page begins the HTML header for the site.
*	The header also creates the right-hand column.
*	This page calls session_start().
*/
require_once('WidgetShoppingCart.php');
require_once "./services/webservices.php"; 

function checkimage($url)
{
   if(@fopen($url,"r") == false)
   {
       $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/images/noimage.jpg';       
   }
   return $url; 
}   
function imageresize($target, $url) 
{   
     
    if(@fopen($url,"r") == false)
    {
       $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/images/noimage.jpg';       
    }
     
    $dim_array = getimagesize($url);  
    $height = $dim_array[1]; 
    $width = $dim_array[0];  
     //gets the new value and applies the percentage, then rounds the value
    $width = round($width * $target);
    $height = round($height * $target);
    
  
    return "<img src=\"$url\"width=\"$width\" height=\"$height\"></a>";
}
function imageresizedimensions($gw_id, $target, $url) 
{    
    if(@fopen($url,"r") == false)
    {
       $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/images/noimage.jpg';       
    }
       
    $dim_array = getimagesize($url);
    $height = $dim_array[1];
    $width = $dim_array[0];  
     //gets the new value and applies the percentage, then rounds the value
    $width = round($width * $target);
    $height = round($height * $target);
    return "<img id= \"$gw_id\" src=\"$url\"width=\"$width\" height=\"$height\"></a>";
  
}
 
 
// Check for a $page_title value:
if (isset($_GET['page_title'])) 
{
    $page_title = $_GET['page_title'];
}
elseif(isset($_GET['category']))
{
    $page_title = $_GET['category'] . " Page";
}

include('header.php');
?>


	<div class="newsbar">
		<h1><b>Browse Main Categories</b></h1>
		<div class="p2"><ul>
<?php 

  
 



$getRequest = new itemcategory;   
$getResponse = $getRequest->getmaincategories();
               
$xml = new SimpleXMLElement($getResponse);

$item_count=$xml->Item_Count;

if ($item_count > 0)
{
    foreach($xml->Item as $eachItem)
    {
        $category_id = $eachItem->category_id;
        $category = $eachItem->category;
        $parent_category_id = (int)$eachItem->parent_category_id;
        // Print as a list item.
        if($parent_category_id != "" || $parent_category_id = 0)
        {
            
	        echo "<li><a href=\"category.php?page=1&category=$category&cid=$category_id&parent_category_id=$parent_category_id&parent_category_name=$parent_category_name\">$category</a></li>\n";    
        }
        else
        {    
            echo "<li><a href=\"category.php?page=1&category=$category&cid=$category_id&parent_category_id=&parent_category_name=\">$category</a></li>\n";          
        }
    }

} // End of if
		
?></ul></div>
		                                                       
<h1><b>Cart Contents</b></h1></a>
      
<div class="p2">
        
<?php
       

if (isset($_GET['do']) && ($_GET['do'] == 'add') ) 
{ // Add new item.
        
    if(isset($_SESSION['cart']))
    {
                       
        $cart=unserialize($_SESSION['cart']); 
                 
    }
    else
    {          
        $cart = new WidgetShoppingCart;
                     
    } 
  
 
        if (isset($_GET['gw_id'])) 
        { // Check for a product ID.
       
                // Typecast to an integer:
               $this_sw_id = (string) $_GET['sw_id'];
               $sw_id = $this_sw_id;
                $gw_id = (int)$_GET['gw_id'];
             
            
           
            // If it's a positive integer,
            // get the item information:
           
            if ($gw_id > 0) 
            {
               
                
                $getRequest = new item();
                $getRequest->gw_id = $gw_id;
                $getResponse = $getRequest->getitembyid(); 
                $xml = new SimpleXMLElement($getResponse);
                if($this_sw_id != "")
                {   
                  
                    foreach ($xml->Item as $ItemXml)
                     {   
                          
                                    
                         $gw_id = (int) $ItemXml->gw_id;
                         $gw_name = (string) $ItemXml->gw_name;
                         $this_gw_name = (string)$gw_name;
                        
                         $this_general_name = (string)$gw_name;
                         $category_id = (int) $ItemXml->category_id;
                         $category  = (string) $ItemXml->category;
                         $general_description = (string) $ItemXml->general_description;
                         $description = (string) $ItemXml->description;
                         
                        
                         
                    }
                            
                         $gw_id = (int)$gw_id;
                        
                            $descriptor_string = "";
                            $xml_build = "<xml>";       
                            $xml_build .= "<gw_name>" . $gw_name . "</gw_name>"; 
                            $getrequest6 = new itemspecialization();
                            $getrequest6->sw_id = $this_sw_id;
                            $getresponse6 = $getrequest6->getisdidbyspecificwidgetid();
                            $xml6 = new SimpleXMLElement($getresponse6);
                           
                            foreach($xml6->Item as $eachitem6)
                            {
                                
                                $isd_id = (string) $eachitem6->isd_id;
                                $isd_id = (string)$isd_id;
                                $xml_build .= "<Item>";
                                $xml_build .= "<isd_id>" . $isd_id . "</isd_id>";
                                
                                
                                $getrequest_sub1 = new itemspecialization();
                                $getrequest_sub1->isd_id = $isd_id;
                                $getresponse_sub1 = $getrequest_sub1->getitemspecializationdescriptorbyisdid();
                                $xml_sub1 = new SimpleXMLElement($getresponse_sub1);
                              
                                foreach($xml_sub1->Item as $eachitem_sub1)
                                {
                                    $descriptor = (string) $eachitem_sub1->descriptor;
                                    
                                    $xml_build .= "<descriptor>" . $descriptor . "</descriptor>";
                                    $temp_isc_id = (string) $eachitem_sub1->isc_id;
                                   
                                    $xml_build .="<isc_id>" . $temp_isc_id . "</isc_id>";
                                    $getrequest_sub2 = new itemspecialization();
                                    $getrequest_sub2->isc_id = $temp_isc_id;               
                                     
                                    $getresponse_sub2 = $getrequest_sub2->getitemspecializationcategorybyiscid();
                                    $xml_sub2 = new SimpleXMLElement($getresponse_sub2);
                                    foreach($xml_sub2->Item as $eachitem_sub2)
                                    {   
                                        $temp_category = (string) $eachitem_sub2->category;
                                     
                                        $xml_build .= "<category>" . $temp_category . "</category>";
                                    } 
                                
                                    $getrequest7 = new itemspecialization();
                                    $getrequest7->isd_id = $isd_id;
                                    $getresponse7 = $getrequest7->getitemspecializationdescriptorbyisdid();
                                    $xml7 = new SimpleXMLElement($getresponse7);
                                    foreach($xml7->Item as $eachitem7)
                                    {
                                        $descriptor = (string) $eachitem7->descriptor;
                                           
                                    }
                                
                                    $descriptor_string .= "[" . $descriptor . "]";
                                } 
                                $xml_build .= "</Item>";    
                            }
                    
                         $xml_build .= "</xml>";
                                      
                         $gw_name = urlencode($xml_build); 
                         $getrequest_price = new itemspecialization();
                         $getrequest_price->sw_id = $sw_id;
                                       
                         $getresponse_price = $getrequest_price->getspecificwidgetbyswid();       
                         $xml_price = new SimpleXMLElement($getresponse_price);
                         $test_num = 0;
                         foreach($xml_price->Item as $eachitem_price)
                         {
                            $price = (double) $eachitem_price->price; 
                            $price = number_format($price, 2, '.', '');  
                            $qty = (string) $eachitem_price->qty;
                            $image = (string) $eachitem_price->image;
                            $weight = (double) $eachitem_price->weight;  
                            $test_num++;
                         }  
                                                                                                                                                                                                                                                                            
                        $array=array('gw_id' => $gw_id, 'sw_id' => $sw_id, 'name' => $gw_name, 'category_id' => $category_id, 'category' => $category, 'general_description' => $general_description, 'image' => $image, 'price' => $price, 'weight' => $weight);

                         // Add to the cart:
                         $cart->add_item($this_sw_id,  $array);
                         
                         $stringgenerator = new stringgenerator();
                         $randomstring = $stringgenerator->generaterandomstring();
                         $cart->identifier = $randomstring; 
                    
                } 
                else
                {
                     foreach ($xml->Item as $ItemXml)
                     {      
                                    
                         $gw_id = $ItemXml->gw_id;
                         $gw_name = $ItemXml->gw_name;
                         $category_id = $ItemXml->category_id;
                         $category  = $ItemXml->category;
                         $general_description = $ItemXml->general_description;
                         $description = $ItemXml->description;
                         $image = $ItemXml->image;
                         $price = $ItemXml->price;
                         $in_stock = $ItemXml->in_stock;
          
                         $weight = $ItemXml->weight;
                     }
                     
                     $gw_id = (string)$gw_id;
                     $gw_name = (string)$gw_name;
                     $sw_id = "";
                     $category_id =  (string)$category_id;
                     $category = (string)$category;
                     $general_description = (string)$general_description;
                     $description = (string)$description;
                     $image = (string)$image;
                     $price = (double)$price;
                     $price = number_format($price, 2, '.', '');    
                     $in_stock = (string)$in_stock;
                     $weight = (double)$weight;
                     
                    
                      
     
                     $array=array('gw_id' => $gw_id, 'sw_id' => $sw_id, 'name' => $gw_name, 'category_id' => $category_id, 'category' => $category, 'general_description' => $general_description, 'image' => $image, 'price' => $price, 'weight' => $weight);
                    
                        

                     $cart->add_item($gw_id,  $array);
                     
                     $cart->add_identifier();
                }

            }  //End of ($sw_id > 0) 
            
       } // End of isset($_GET['sw_id']) IF.  
       $_SESSION['cart']=serialize($cart);
}
           
if (isset($_SESSION['cart']))     
{       
    $cart=unserialize($_SESSION['cart']);  
        
    print "<ul>";
    $total = 0.00; 
    //loop thru each item in the cart, print out those items as hyperlinks, also sum up total for items in cart and set that as session data known as total (line 202)
    foreach ($cart->items as $id => $info) 
    {
                     
        $price = $info['price'];
        $subtotal = $info['qty'] * $info['price']; 
        $total += $subtotal;
                 
        $array = $cart->items;
        $category_id = $info['category_id'];
        $category = $info['category'];
        $gw_id = $info['gw_id'];
        
        $sw_id = $info['sw_id'];           
        $getreq = new item();
        $getreq->gw_id = $gw_id;
        $getres = $getreq->getitembyid();
        $x = new SimpleXMLElement($getres);
        foreach($x->Item as $each)
        {
            $general_name = $each->gw_name;
        } 
        $name = $info['name'];  
        $qty = $info['qty'];  
                   
        $getrequest = new itemcategory();
        $getrequest->category_id = $category_id;
        $getresponse = $getrequest->getcategorybycategoryid();
        $xml = new SimpleXMLElement($getresponse);
        $parent_category_id = "";
        $parent_category = "";
        foreach($xml->Item as $eachitem)
        {
            $parent_category_id = $eachitem->parent_category_id;    
        }
        if($parent_category_id != "")
        {
            $getrequest = new itemcategory();
            $getrequest->category_id = $parent_category_id;
            $getresponse = $getrequest->getcategorybycategoryid();
            $xml = new SimpleXMLElement($getresponse);
            foreach($xml->Item as $eachitem)
            {
                $parent_category = $eachitem->category;    
            }
            
        }
            
                    
          
            if($sw_id == "")
            { 
                echo'<div class="colorlightgrey">';    
              
                ?>
                <a href="./product.php?page_title=Product Page&gw_id=<?php echo $gw_id ?>&cid=<?php echo $category_id?>&category=<?php echo $category?>&parent_category_id=<?php echo $parent_category_id?>&parent_category=<?php echo $parent_category ?>"><b><?php echo $general_name ?></b></a>                                                                                                                            
                <div class="colorlightergrey">
                <tr>
                <?php
                echo '<td>';
                echo '<b>Price:</b>$'; 
                echo $price . "/unit";
                echo '</td>';
                echo '<td>';
                echo '<br /><b>Quantity: </b>';
                echo $qty;
                echo '</td>';
                echo '</div>';
                echo '</div>';
                echo '<br />';
                
                
            }
            else
            {
                $name = urldecode($name);     
                $name_xml = new SimpleXMLElement($name);
                echo'<div class="colorlightgrey">';          
                echo '<td>';
                echo '<td>';
                ?>
                <a href="./product.php?page_title=Product Page&gw_id=<?php echo $gw_id ?>&cid=<?php echo $category_id?>&category=<?php echo $category?>&parent_category_id=<?php echo $parent_category_id?>&parent_category=<?php echo $parent_category ?>"><b><?php echo $general_name ?></b></a>                  
                <?php
                echo '</td>';
                echo '<form action="product.php" method="GET" id="general_widget">';
                echo '<div class="colorlightergrey">';  
                     
                foreach($name_xml->Item as $eachitem_desc)
                {
                    $this_category = $eachitem_desc->category; 
                    $this_descriptor = $eachitem_desc->descriptor;  
                        ?>
                     <tr>
                        <?php

                        echo '<td>';
                        echo "<b>" . $this_category . ":</b>" . $this_descriptor . "<br />";
                        echo '</td>';
                         } 
                        echo '<td>';
                        echo '';
                        echo '<b>Price:</b>$' . $price . "/unit";
                        echo '</td>';
                        echo '<td>';
                        echo '<br />';
                        echo '<b>Quantity:</b>' . $qty;
                        echo '</td>'; 
                        echo '<td>';
                        echo '<input type="hidden" id="page_title" name="page_title" value="Product Page">';
                        echo '<input type="hidden" id="gw_id" name="gw_id" value="' . $gw_id . '">';
                        echo '<input type="hidden" id="cid" name="cid" value="' . $category_id . '">';
                        echo '<input type="hidden" id="category_id" name="category_id" value="' . $category_id . '">';
                        echo '<input type="hidden" id="category" name="category" value="' . $category . '">';
                        echo '<input type="hidden" id="parent_category_id" name="parent_category_id" value="' . $parent_category_id . '">';  
                        echo '<input type="hidden" id="parent_category" name="parent_category" value="' . $parent_category . '">';  
                        echo '<input type="hidden" id="category" name="category" value="' . $category . '">';  
                        echo '</td>';
                        echo '</tr>';
                        echo '</form>';
                        
                         echo '</div>'; 
                         echo '</div>';
                         
                         //echo '</table>';
                         echo '<br />'; 
                     
                     } 
                }  
    
    echo ' <br /><table align="right"><tr align="right"><td align="right"><a href="cart.php"><img src="./images/go_to_cart.jpg?page_title=cart"></td></tr></table><br />';

    echo '</li>';
    print "</ul>";
    $_SESSION['total'] = $total;     
    
    



    
    
    
    
    
                                      
$_SESSION['cart'] = serialize($cart);
} 
         
          
?>
</div>
		
<h1><b>Total</b></h1> 
<h2>
    <b> 
        <?php 
        //if session variable 'total' is set then print it for the user to see while shopping. 
        if(isset($_SESSION['total']))
        {
            $total =  number_format($_SESSION['total'], 2, '.', '');  
            print "$"; 
            print  $total;
        } 
        
        
        ?>
    </b>
</h2>
		<div class="p2"> 
			
		</div>
	     
	</div>
	
	<div class="content"> 